/*
 * Created on 2006-5-14
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package cn.com.yinhex.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.SimpleNodeIterator;

import ayou.util.DOC;
import ayou.util.EncryptUtil;
import ayou.util.NetUtil;
import ayou.util.StringUtil;

/**
 * @author Ayou
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class DownloadPic {

	DOC mapping = new DOC();

	public DownloadPic() {
	}

	public String download(String hostid, String body) {
		return download(hostid, body, false);
	}

	public DOC getMapping() {
		return mapping;
	}

	public String download(String hostid, String body, boolean force) {
		try {
			Parser parser = Parser.createParser(body, "UTF-8");
			NodeList list = parser.extractAllNodesThatMatch(new TagNameFilter("img"));
			SimpleNodeIterator iterator = list.elements();
			while (iterator.hasMoreNodes()) {
				ImageTag img = (ImageTag) iterator.nextNode();
				String strhtml = img.toHtml();
				if (body.indexOf(strhtml) > -1) {
					String imgurl = img.getAttribute("src");
					String newurl = download_one(imgurl);
					if (StringUtil.isFine(newurl)) {
						body = StringUtil.replace(body, strhtml, "<img src=\"" + newurl + "\" border=\"0\">");
					}
				}
			}
			insertPics(hostid);
			return body;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return body;
		}
	}

	public String download_one(String imgurl) {
		if (StringUtil.isFine(imgurl)) {
			String hash = EncryptUtil.getMD5(imgurl).toLowerCase();
			String newurl = "http://" + hash.charAt(0) + ".p.sudone.com/" + hash + ".pic";
			if (NetUtil.getHost(imgurl).indexOf(".p.sudone.com") == -1 && NetUtil.getHost(imgurl).indexOf(".p.aiganggu.com") == -1) {
				System.out.println(imgurl);
				mapping.put(imgurl, newurl);
				return newurl;
			}
		}
		return imgurl;
	}

	public void insertPics(String hostid) {
		String[] imgurls = mapping.getKeys();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; imgurls != null && i < imgurls.length; i++) {
			sb.append(imgurls[i]);
			sb.append(",");
		}
		insertPic(hostid, sb.toString());
	}

	public static void insertPic(String hostid, String urls) {
		if (StringUtil.isFine(urls)) {
			String hash = EncryptUtil.getMD5("a" + hostid + urls + "@ayou.in");
			HttpClient httpClient = new HttpClient();
			PostMethod postMethod = new PostMethod("http://s.sudone.com/insertpic.php");
			NameValuePair[] data = { new NameValuePair("hostid", "a" + hostid), new NameValuePair("urls", urls), new NameValuePair("hash", hash) };
			postMethod.setRequestBody(data);
			try {
				httpClient.executeMethod(postMethod);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				postMethod.releaseConnection();
			}
		}
	}

	public static String getPicURL(String url) {
		String hash = EncryptUtil.getMD5(url).toLowerCase();
		return "http://" + hash.charAt(0) + ".p.sudone.com/" + hash + ".pic";
	}
}
